package org.duang.pay.core;

import java.io.InputStream;
import java.util.Map;

import org.duang.common.exceptios.ServiceException;
import org.duang.config.Config;
import org.duang.http.base.HttpRes;
import org.duang.kit.HttpKit;
import org.duang.kit.ObjectKit;
import org.duang.kit.ToolsKit;
import org.duang.pay.utils.SignUtils;

/**
 * 支付中间件工厂类
 * @author laotang
 *
 */
public class PayFactory {
	
	public final static String SIGN_KEY_FIELD = "sign";
	public final static String PAY_TYPE_KEY_FIELD = "paytype";
	
	public static final String RESULT_API_METHOD_URI = "/common/pay/result";
	public static final String SIGN_API_METHOD_URI = "/common/pay/sign";	
	public static final String REDPACK_METHOD_URI = "/common/pay/redpack";
	public static final String HBINFO_METHOD_URI = "/common/pay/hbinfo";
	
	public static String getAppId() {
		return Config.getPropertie("pay.appid", "f0d348782e664f92ae8425691b54be6d");
	}
	
	
	public static String getResult(IPayRequest payRequest) {
		if(ToolsKit.isEmpty(payRequest.getAppid())){
			throw new NullPointerException("PayClientFactory getResult:  appId is null");
		}
		InputStream value = PayFactory.getBody(payRequest);
		String url = Config.getPayHost() + RESULT_API_METHOD_URI;
//		String url = "http://192.168.0.159:8083" + RESULT_API_METHOD_URI;
		try{
			return exceute(url, value);
		} catch(Exception ex) {
			ex.printStackTrace();
			return "";
		}
	}
	
	private static InputStream getBody(IPayRequest request) {
		Map<String, Object> map = ObjectKit.getFieldMap(request);
		if(ToolsKit.isEmpty(map)) throw new NullPointerException("PayClientFactory getBody:  map is null");
		String paytype = map.get(PAY_TYPE_KEY_FIELD).toString();
		if(ToolsKit.isEmpty(paytype)) throw new ServiceException("paytype is null");
		map.remove(SIGN_KEY_FIELD);
		return ToolsKit.string2InputStream(ToolsKit.toJsonString(map), Config.getCharEncode());
	}
	
	private static InputStream getBody(Map<String,Object> map) {
		if(ToolsKit.isEmpty(map)) throw new NullPointerException("PayClientFactory getBody:  map is null");		
		return ToolsKit.string2InputStream(ToolsKit.toJsonString(map), Config.getCharEncode());
	}
	
	public static String sign(Map<String,Object> map) {
		if(ToolsKit.isEmpty(map)) throw new NullPointerException("PayClientFactory sign:  map is null");		
//		String url = "http://192.168.0.159:8083" + SIGN_API_METHOD_URI;
		String url = Config.getPayHost() + SIGN_API_METHOD_URI;
//		String new_sign = SignUtils.buildSign(map, "MD5", appKey, Config.getCharEncode());
		try{
			return exceute(url, getBody(map));
		} catch(Exception e) {
			e.printStackTrace();
			return "";
		}
	}
	/*
	public static boolean sign(IPayRequest payRequest) {
		Map<String, Object> map = ObjectKit.getFieldMap(payRequest);
		if(ToolsKit.isEmpty(map)) throw new NullPointerException("PayClientFactory sign:  map is null");		
		return sign(map);
		
//		String url = "http://192.168.0.159:8083" + SIGN_API_METHOD_URI;
//		String url = Config.getPayHost() + SIGN_API_METHOD_URI;
//		String new_sign = SignUtils.buildSign(map, "MD5", appKey, Config.getCharEncode());
//		try{
//			return Boolean.parseBoolean(exceute(url, getBody(map)));
//		} catch(Exception e) {
//			e.printStackTrace();
//			return false;
//		}
//		return sign.equals(new_sign);
	}
	*/
	public static String redpack(IPayRequest payRequest) {
		Map<String, Object> map = ObjectKit.getFieldMap(payRequest);
		if(ToolsKit.isEmpty(map)) throw new NullPointerException("PayClientFactory checkSign:  map is null");		
//		String url = "http://127.0.0.1:8083" + REDPACK_METHOD_URI;
		String url = Config.getPayHost() + REDPACK_METHOD_URI;
		try{
			String resultStr = exceute(url, getBody(map));
			if(!"SUCCESS".equalsIgnoreCase(resultStr)) {
				throw new ServiceException().setCode(500).setMessage(resultStr);
			}
			return  resultStr;
		} catch(Exception e) {
			e.printStackTrace();
			return e.getMessage();
		}
	}
	
	
	public static String hbinfo(IPayRequest payRequest) {
		Map<String, Object> map = ObjectKit.getFieldMap(payRequest);
		if(ToolsKit.isEmpty(map)) throw new NullPointerException("PayClientFactory hbinfo:  map is null");		
//		String url = "http://192.168.0.40:8083" + HBINFO_METHOD_URI;
		String url = Config.getPayHost() + HBINFO_METHOD_URI;
//		String new_sign = SignUtils.buildSign(map, "MD5", appKey, Config.getCharEncode());
		try{
			return exceute(url, getBody(map));
		} catch(Exception e) {
			e.printStackTrace();
			return "";
		}
	}
	
	private static String exceute(String url, InputStream value) {
		if(ToolsKit.isNotEmpty(value)){
			HttpRes res = HttpKit.duang().url(url).body(value).security(true).post();
			if(res.getStatusCode() == 200) {
				return res.getResult();
			} else {
				throw new ServiceException().setMessage(res.getException().getMessage());
			}
		} 
		throw new ServiceException().setMessage("InputStream is null");
	}
}
